package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dtos.ModuloDTO;
import dtos.OrdenDespachoDTO;
import dtos.ServiciosModuloDTO;
import dtos.SolicitudDTO;

/**
 * Servlet implementation class BuscarOrdenes
 */
@WebServlet("/BuscarOrdenes")
public class BuscarOrdenes extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	private bd.BusinessDelegate bd = null;
	private List<OrdenDespachoDTO> resultado = null;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BuscarOrdenes() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.println("<table width='100%' cellspacing='5' cellpading='5'>");
		resultado = bd.getInstance().getOrdenesDespacho();
		String servicios= "";
		for(OrdenDespachoDTO aux : resultado){
			out.println("<tr width='100%'>");
			out.println("<td width='5%'>"+aux.getCodigoDespacho()+"</td>");
			out.println("<td width='20%'>"+aux.getEstados().get(aux.getEstados().size()-1).getValor()+"</td>");
			out.println("<td width='10%'>"+aux.getIdModuloPortal()+"</td>");
			out.println("</tr>");
			out.println("<table width='100%' cellspacing='5' cellpading='5'>");
			out.println("<tr width='100%'>");
			out.println("<td colspan='5'>SOLICITUDES</td>");
			out.println("</tr>");
			out.println("<tr width='100%'>");
			out.println("<td style='font-weight:bold; line-height:0.5em;'><img src='images/marker.gif' border='0'></img> Id Solicitud</td>");
			out.println("<td style='font-weight:bold; line-height:0.5em;'><img src='images/marker.gif' border='0'></img> Deposito</td>");
			out.println("<td style='font-weight:bold; line-height:0.5em;'><img src='images/marker.gif' border='0'></img> Estado</td>");
			out.println("<td style='font-weight:bold; line-height:0.5em;'><img src='images/marker.gif' border='0'></img> Codigo Orden Compra</td>");
			out.println("</tr>");
			for(SolicitudDTO saux : aux.getSolicitudes()){
				out.println("<td>"+saux.getId()+"</td>");
				out.println("<td>"+saux.getDeposito()+"</td>");
				out.println("<td>"+saux.getEstados().get(saux.getEstados().size() -1).getEstado()+"</td>");
				out.println("<td>"+aux.getCodigoOrdenVenta()+"</td>");
			}
			out.println("</tr>");
			out.println("</table>");
		}
		out.println("</table>");
	}
}
